<?php
/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/7
 * Time: 10:21
 * 库存管理
 */
class stock extends CI_Controller {
    public function __construct()
    {
        parent::__construct();

        $this->load->model('stock_model');
        $this->load->model('goods_model');
    }

    /**
     * 商品列表
     */
    public function index(){

        $page_num = '4';//每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $this->input->get('search'),
                'side' => 'both'
            ];

            $data = $this->goods_model->search('*',0,$searchArg,$page_num,$start_position);
//            print_r($data);
//            die();
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => count($data['total_num']), //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
            $this->load->view('/admin/stock/list',$viewData);
        } else {

//            print_r(123);
//            die();
            $data = $this->goods_model->page('goods',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            $this->load->view('/admin/stock/list',$viewData);
        }

    }


    //编辑功能
    public function edit(){

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $result['stockList'] = $this->stock_model->getByFieldQueryData('*',$arg,0);

        $this->load->view('/admin/stock/edit',$result);
    }

}